package com.mengdie.shuidi.ui.adapter;

import android.graphics.Color;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.mengdie.shuidi.R;
import com.mengdie.shuidi.event.AdapterEvent;
import com.mengdie.shuidi.model.ChildMsg;
import com.mengdie.shuidi.model.Level0Item;

import org.greenrobot.eventbus.EventBus;

import java.util.List;


public class SystemMsgAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {


    public static final int TYPE_LEVEL_0 = 0;
    public static final int TYPE_PERSON = 1;


    public SystemMsgAdapter(List<MultiItemEntity> data) {
        super(data);
        addItemType(TYPE_LEVEL_0, R.layout.item_expandable_lv0);
        addItemType(TYPE_PERSON, R.layout.item_expandable_lv1);
    }

    @Override
    protected void convert(final BaseViewHolder helper, MultiItemEntity item) {
        switch (helper.getItemViewType()) {
            case TYPE_LEVEL_0:
                final Level0Item father = (Level0Item) item;
                helper.setText(R.id.tv_title, father.getTitle())
                        .setText(R.id.tv_date, father.getSend_date())
                        .setImageResource(R.id.iv_index, father.isExpanded() ? R.drawable.line_up : R.drawable.line_down);
                TextView tvTitle = helper.getView(R.id.tv_title);
                TextView tvDate = helper.getView(R.id.tv_date);
                boolean read = father.getReadFlag().equals("0");
                if (read) {
                    tvTitle.setTextColor(Color.parseColor("#000000"));
                    tvDate.setTextColor(Color.parseColor("#000000"));
                } else {
                    tvTitle.setTextColor(Color.parseColor("#c6c6c6"));
                    tvDate.setTextColor(Color.parseColor("#c6c6c6"));
                }

                RelativeLayout rlBg = helper.getView(R.id.rl_bg);
                TextView tvLogo = helper.getView(R.id.tv_msg_bg);
                int layoutPosition = helper.getLayoutPosition();
                if (layoutPosition == 0) {
                    rlBg.setVisibility(View.VISIBLE);
                    String title = father.getTitle();
                    String substring = title.substring(0, 1);
                    tvLogo.setText(substring);
                } else {
                    rlBg.setVisibility(View.GONE);
                }
                helper.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        final int pos = helper.getAdapterPosition();
                        if (father.isExpanded()) {
                            collapse(pos);
                        } else {
                            expand(pos);
                            EventBus.getDefault().post(new AdapterEvent(father,pos));
                        }
                    }
                });
                break;
            case TYPE_PERSON:
                final ChildMsg childMsg = (ChildMsg) item;
                helper.setText(R.id.tv_child_title, childMsg.getTitle())
                        .setText(R.id.tv_child_desc, childMsg.getContent());
                TextView tvChildTitle = helper.getView(R.id.tv_child_title);
                TextView tvChildDesc = helper.getView(R.id.tv_child_desc);
                boolean childRead = childMsg.getRead_flag().equals("0");
                if (childRead) {
                    tvChildTitle.setTextColor(Color.parseColor("#000000"));
                    tvChildDesc.setTextColor(Color.parseColor("#000000"));
                } else {
                    tvChildTitle.setTextColor(Color.parseColor("#c6c6c6"));
                    tvChildDesc.setTextColor(Color.parseColor("#c6c6c6"));
                }

                break;
        }
    }

}
